#ifndef FRAME_DB_H_
#define FRAME_DB_H_
#pragma once
#include "utilities_all.h"




//
//typedef struct ThreadCfg
//{
//	unsigned int drivebararrcnt;
//	BarInfo* drivebararr;
//
//}ThreadCfg;
//
//
//typedef struct MultiThreadCfg
//{
//	unsigned int threadcfgcnt;
//	ThreadCfg threadcfgarr[MAX_FRAMEDB_THREADCNT];
//}MultiThreadCfg;


//
//typedef enum Timertype
//{
//	invalid = 0, dbtimer = 1, apptimer, mdmqtimer
//}Timertype;

typedef struct TimerData
{
	TimerFire_namespace::TimerFireType type;
}TimerData;


typedef struct ThreadCom
{
	Mq* pmq;
	HANDLE han;
	string hanid;
}ThreadCom;

class CFrameDB
{
public:

	//CFrameDB(ThreadCfg* m_pthreadcfg);

	CFrameDB(vector<string> &threadInsts);

	virtual ~CFrameDB();

	virtual void Frame();

	void ReleaseMdSemAndSetData(CThostFtdcDepthMarketDataField *pDepthMarketData);

	void ReleaseTimerSemAndSetData(TimerData* pdata);

	void ReleaseTerminateSem();

	void SubscribeMarketData();

	void UnSubscribeMarketData();

private:

	bool InitTimer();

	void DestroyTimer();;

	void ResetTimer();

	bool InitMd();

	void DestroyMd();

	void ResetMd();

	bool InitBroad();

	void DestroyBroad();

	bool InitDB();

	void DestroyDB();

	bool InitTerminate();

	void DestroyTerminate();

	void BroadOperation(MarketData* ptick);


	void Md_Frame();

	void Timer_Frame();

	void OnTimer_DB();

	void OnTimer_APP();

	void OnTimer_MDMQ();

	

public:

	//ThreadCfg* m_pthreadcfg;

	vector<string> m_vThreadInst;

private:

	//used to store td mq
	ThreadCom m_mdcom;

	//used to store MFC related
	ThreadCom m_timercom;



	Mq* m_md_mq;
	HANDLE m_han[3];

	map<string, Broadcast*> m_mapbroad;
	typedef map<string, Broadcast*>::iterator mapbroad_it;

	map<string, DB*> m_mapdb;
	typedef map<string, DB*>::iterator mapdb_it;

	

///////////////////////////////////static//////////////////////////////////////////
private:

	static CMd* m_pMd;

	
public:

	static bool Initpmd(char* pborkeid, char* puserid, char* ppassword, char* pMDFront, unsigned int flag);

	static bool Destroypmd();

	static bool GetMdConnectState();

/////////////////////////////////////////////////////////////////////////////////////


};
#endif